from gluon import *

def use(lib, a='js', c='static'):
    libs = lib if type(lib) == list else [lib]
    for js in libs:
        current.response.files.append(URL(a,c,f=js))

class Session():
    """
    A Python object class to help with passing JavaScript session data in c9r.session.
    """
    data = {}

    def __init__(self, md={}):
        self.update(md)

    def json(self):
        return current.response.json(Session.data)

    def render(self):
        return SCRIPT("var c9r = c9r || {};\n"
                      "$.extend(c9r, function () {"
                      "  var public = { pagedata: %s };\n"
                      "  $(document).ready(c9r.page_init);"
                      "  return public;"
                      "}());" % self.json())

    def update(self, md):
        if (len(md) > 0):
            Session.data.update(md)
